import time

from selenium.webdriver.common.by import By

from base.base_page import BasePage
from utils_tools import DriverTools


class addQueryGoods(BasePage):

    # 登录admin2街角的1
    def login_admin(self):
        self.get_url("http://hmshop-test.itheima.net/index.php/Admin/Admin/login")
        self.element_send_keys(By.CLASS_NAME, "input-text", "admin")
        self.element_send_keys(By.NAME, "password", "123456")
        self.element_send_keys(By.NAME, "vertify", "8888")
        self.element_click(By.NAME, "submit")

    # 添加商品
    def add_goods(self, goods, price1, price2):
        self.element_click(By.CSS_SELECTOR, "li[data-param='goods']")
        self.element_click(By.LINK_TEXT, "商品列表")
        self.element_iframe(By.ID, "workspace")
        self.element_click(By.CSS_SELECTOR, "div[class='add'][title='添加商品']")
        self.element_send_keys(By.CLASS_NAME, "input-txt", goods)
        self.element_down_index(By.ID, "cat_id", 5)
        self.element_send_keys(By.NAME, "shop_price", price1)
        self.element_send_keys(By.NAME, "market_price", price2)
        self.element_click(By.ID, "is_free_shipping_label_1")
        self.element_click(By.ID, "submit")

    # 在首页查看商品是否已添加并返回文本
    def query_goods(self, goods):
        self.get_url("http://hmshop-test.itheima.net/")
        self.element_send_keys(By.ID, "q", goods)
        self.element_click(By.CLASS_NAME, "ecsc-search-button")
        goods = self.get_element_text(By.CSS_SELECTOR, "div[class='shop_name2']")
        price1 = self.get_element_text(By.CSS_SELECTOR, "span[class='now']")
        price2 = self.get_element_text(By.CSS_SELECTOR, "span[class='old']")
        return goods, price1, price2

    # 删除已添加的商品
    def delete_goods(self, goods):
        self.login_admin()
        self.element_click(By.CSS_SELECTOR, "li[data-param='goods']")
        self.element_click(By.LINK_TEXT, "商品列表")
        self.element_iframe(By.ID, "workspace")
        self.element_send_keys(By.NAME, "key_word", goods)
        self.element_click(By.CSS_SELECTOR, "input[type='button']")
        self.element_move_to(By.CSS_SELECTOR, "span[class='btn']")
        self.element_click(By.LINK_TEXT, "删除商品")
        self.element_click(By.CLASS_NAME, "layui-layer-btn0")

    # 退出登录
    def login_out(self):
        self.element_default_content()
        self.element_move_to(By.CLASS_NAME, "bgdopa-t")
        self.element_click(By.CLASS_NAME, "login-out")

    # 连接各个板块
    def add_goods_query(self, goods, price1, price2):
        self.login_admin()
        self.add_goods(goods, price1, price2)
        self.login_out()
        text =  self.query_goods(goods)
        self.delete_goods(goods)
        self.login_out()
        return text


if __name__ == '__main__':
    a = addQueryGoods()
    text = a.add_goods_query("0002221", "0.99", "99999.99")
    print(text)
    DriverTools.quit_driver()